package packeter

import "github.com/project-flogo/core/data/coerce"

type Settings struct {
	OutputChannel     string `md:"outputChannel" json:"outputChannel,omitempty"`
	OutputChannelSize int    `md:"outputChannelSize" json:"outputChannelSize,omitempty"`
	MinOutputCount    int    `md:"minOutputCount" json:"minOutputCount,omitempty"`
	MaxOutputCount    int    `md:"maxOutputCount" json:"maxOutputCount,omitempty"`
}

type EdgeChannelData map[string]interface{}

type Input struct {
	Action string          `md:"action" json:"action,omitempty"`
	Datum  EdgeChannelData `md:"datum" json:"datum,omitempty"`
}

func (r *Input) ToMap() map[string]interface{} {
	return map[string]interface{}{
		"action": r.Action,
		"datum":  r.Datum,
	}
}

func (r *Input) FromMap(values map[string]interface{}) error {
	action, err := coerce.ToString(values["action"])
	if err != nil {
		return err
	}
	r.Action = action

	datum, err := coerce.ToObject(values["datum"])
	if err != nil {
		return err
	}
	r.Datum = datum

	return nil
}

type Output struct {
	Count int    `md:"count" json:"count"`
	Err   string `md:"err" json:"err,omitempty"`
}

func (r *Output) ToMap() map[string]interface{} {
	return map[string]interface{}{
		"count": r.Count,
		"err":   r.Err,
	}
}

func (r *Output) FromMap(values map[string]interface{}) error {
	count, err := coerce.ToInt(values["count"])
	if err != nil {
		return err
	}
	r.Count = count

	errStr, err := coerce.ToString(values["err"])
	if err != nil {
		return err
	}
	r.Err = errStr

	return nil
}
